<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户列表</title>
    <script src="js/vue.js"></script>
    <script src="js/axios.js"></script>
    <style>
        * {
            margin: 0;
            padding: 0;
            list-style: none;
        }

        .table {
            width: 500px;
        }

        .table .item {
            float: left;
            width: 100px;
            height: 40px;
            text-align: center;
            line-height: 40px;
            background-color: black;
            color: burlywood;
        }
    </style>
</head>

<body>
    <div id="app">
        <div>
            <div>用户名<input v-model="username" /></div>
            <div>密码<input v-model="userpwd" /></div>
            <button @click="add">新增</button>
        </div>
        <div class="table">
            <div class="tr">
                <div class="item">编号</div>
                <div class="item">用户名</div>
                <div class="item">密码</div>
                <div class="item">操作</div>
                <div class="item">操作</div>
            </div>
            <div class="tr" v-for="item in list">
                <div class="item">{{item.id}}</div>
                <div class="item">{{item.username}}</div>
                <div class="item">{{item.userpwd}}</div>
                <div class="item" @click="del(item.id)">删除</div>
                <div class="item" @click="edit(item)">编辑</div>
            </div>
        </div>

        <div class="dialog" v-show="flag">
            <h3>编辑信息</h3>
            <div>
                用户名 <input v-model="editusername" />
            </div>
            <div>
                密码 <input v-model="edituserpwd" />
            </div>
            <button @click="save">确认</button> <button @click="cancel">取消</button>

        </div>

    </div>
</body>
<script>
    new Vue({
        el: "#app",
        data: {
            flag: false,//控制弹出层
            username: "",
            userpwd: "",
            editusername: "",
            edituserpwd: "",
            eidtid: "",
            //编辑的用户信息
            list: [],//用户集合

        },
        methods: {
            async add() {

                //判断用户名是否存在(查询)
                let { data: list } = await axios({
                    method: "get",
                    url: "http://localhost:2346/userinfos",
                    params: {
                        username: this.username
                    }
                })
                //查询用户名是否存在
                if (list.length) {
                    alert("该用户名已经存在");
                    return false;
                }

                let { data } = await axios({
                    method: "post",
                    url: "http://localhost:2346/userinfos",
                    data: {
                        username: this.username,
                        userpwd: this.userpwd
                    }
                })
                //新增
                this.list.push(data);
            },
            async del(id) {
                await axios({
                    method: "delete",
                    url: "http://localhost:2346/userinfos/" + id
                })

                this.list = this.list.filter(item => item.id != id);


            },
            edit(info) {//info 点击的对象信息
                this.flag = true;//控制弹出层显示
                this.editusername = info.username;
                this.edituserpwd = info.userpwd;
                this.eidtid = info.id;//当前编辑对象的id
            },
            async save() {//保存
                //查询用户名是否存在  
                //存在的话 看不是自己当前用的  是自己的  就可以更新  不是自己就不能用
                //不存在的话 可以更新
                let username = this.editusername;//取编辑的用户名

                let { data: list } = await axios({
                    method: "get",
                    url: "http://localhost:2346/userinfos",
                    params: {
                        username
                    }
                });
                //查询
                if (list.length) { //有数据
                    //看是不是自己
                    let info = list[0];//因为id是组件 用户名也是唯一的  如果能取到数据
                    //也只会有一条数据
                    if (info.id != this.eidtid) {
                        alert("该用户名已经存在");
                        return false;
                    }
                }//没有该用户名  没有该用户名就直接更新

                let res = axios({
                    method: "patch",
                    url: "http://localhost:2346/userinfos/" + this.eidtid,
                    data: {
                        username, userpwd: this.edituserpwd
                    }
                });//根据id 去更新用户的用户名和密码
                //更新页面
                this.list = this.list.map(item=>{
                    if(item.id == this.eidtid){
                        item.username = this.editusername;
                        item.userpwd = this.edituserpwd;
                    }
                    return item;
                })

               this.flag = false;
            },
            cancel() {//取消
                this.flag = false;
            }

        },
        async created() {
            let { data: list } = await axios({
                method: "get",
                url: "http://localhost:2346/userinfos",
                params: {}
            })
            this.list = list;
        }


    })


</script>

</html>